package com.zenithmind.library.pojo.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 图书副本视图对象
 * 
 * @author ZenithMind
 * @since 2024-06-14
 */
@Data
@Schema(name = "图书副本视图对象", description = "图书副本信息的视图对象")
public class BookCopyVO {

    @Schema(description = "副本ID")
    private String id;

    @Schema(description = "图书ID")
    private String bookId;

    @Schema(description = "图书标题")
    private String bookTitle;

    @Schema(description = "图书ISBN")
    private String bookIsbn;

    @Schema(description = "图书封面")
    private String bookCover;

    @Schema(description = "条形码")
    private String barcode;

    @Schema(description = "RFID标签")
    private String rfidTag;

    @Schema(description = "状态")
    private Integer status;

    @Schema(description = "状态名称")
    private String statusName;

    @Schema(description = "位置信息")
    private String location;

    @Schema(description = "书架号")
    private String shelfNumber;

    @Schema(description = "层号")
    private Integer floorNumber;

    @Schema(description = "区域")
    private String area;

    @Schema(description = "完整位置")
    private String fullLocation;

    @Schema(description = "采购价格")
    private BigDecimal purchasePrice;

    @Schema(description = "采购日期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate purchaseDate;

    @Schema(description = "供应商")
    private String supplier;

    @Schema(description = "备注")
    private String remarks;

    @Schema(description = "当前借阅用户ID")
    private String currentBorrowUserId;

    @Schema(description = "当前借阅用户姓名")
    private String currentBorrowUserName;

    @Schema(description = "借阅日期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate borrowDate;

    @Schema(description = "应还日期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate dueDate;

    @Schema(description = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Schema(description = "更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    /**
     * 获取状态名称
     */
    public String getStatusName() {
        if (status == null) {
            return "未知";
        }
        return switch (status) {
            case 0 -> "不可借阅";
            case 1 -> "可借阅";
            case 2 -> "已借出";
            case 3 -> "维修中";
            case 4 -> "丢失";
            case 5 -> "损坏";
            default -> "未知";
        };
    }

    /**
     * 获取完整位置信息
     */
    public String getFullLocation() {
        StringBuilder fullLoc = new StringBuilder();
        if (area != null) {
            fullLoc.append(area);
        }
        if (floorNumber != null) {
            if (fullLoc.length() > 0) fullLoc.append(" ");
            fullLoc.append(floorNumber).append("层");
        }
        if (shelfNumber != null) {
            if (fullLoc.length() > 0) fullLoc.append(" ");
            fullLoc.append(shelfNumber).append("号书架");
        }
        if (location != null && !location.trim().isEmpty()) {
            if (fullLoc.length() > 0) fullLoc.append(" ");
            fullLoc.append(location);
        }
        return fullLoc.toString();
    }

    /**
     * 判断是否可借阅
     */
    public boolean isAvailable() {
        return status != null && status == 1;
    }

    /**
     * 判断是否已借出
     */
    public boolean isBorrowed() {
        return status != null && status == 2;
    }

    /**
     * 判断是否逾期
     */
    public boolean isOverdue() {
        return isBorrowed() && dueDate != null && LocalDate.now().isAfter(dueDate);
    }

    /**
     * 判断是否丢失或损坏
     */
    public boolean isLostOrDamaged() {
        return status != null && (status == 4 || status == 5);
    }

    /**
     * 计算剩余天数
     */
    public int getRemainingDays() {
        if (dueDate == null) {
            return 0;
        }
        return (int) java.time.temporal.ChronoUnit.DAYS.between(LocalDate.now(), dueDate);
    }

    /**
     * 计算逾期天数
     */
    public int getOverdueDays() {
        if (dueDate == null || !isOverdue()) {
            return 0;
        }
        return (int) java.time.temporal.ChronoUnit.DAYS.between(dueDate, LocalDate.now());
    }
}
